<?php
/**
 * 文章控制器
 *
 * @category     Shuxun Cms
 * @date         2015年10月18日
 * @author       Shuxun Team <kaiping.jiang@scshux.com>
 * @file         ArticleController.php
 */
namespace Scshux\Front\Controllers;
use Scshux\Utils\JSON;
use Scshux\Mvc\SModel;
use Scshux\Utils\Uploader;
use Scshux\Mvc\SQuery;
use Scshux\Plugin\Wechat;
use Scshux\Utils\Helper;
use Scshux\Tag\SmartyTag;
use Scshux\Utils\Time;
use Scshux\Utils\Tree;
use Scshux\Utils\Format;
class ArticleController extends  ControllerBase
{
	
	protected  $controller = 'article';
	/**
	 * 文章详细
	 * @date 2015-12-19 招聘信息我也用了文章了 所以这里需要排除
	 */
	public  function  indexAction()
	{
		$cid = $this->request->get('cid','int',0);
		$page = intval($_REQUEST['_page']);
		if(empty($page))$page =1;
		//新增语言过滤
		$site_id = $this->getCore()->getSiteID();
	
		
		//读取文章
		$articleQuery = new SQuery('article as a');
		$articleQuery->join = ' join article_category as c on a.cat_id=c.id ';
		$articleQuery->fields = 'a.*,c.name as cat_name';
		$articleQuery->page = $page;
		$articleQuery->pagesize = 5;
		$articleQuery->order = 'a.sort desc,a.id desc';
		if($cid){
			$articleQuery->where ='  a.cat_id ='.$cid . ' and a.site_id='.$site_id;
		}else{
			$articleQuery->where = 'a.cat_id != 11 and a.site_id='.$site_id;
		}
		$articleItems = Format::TimeFormat($articleQuery->find(),'created');
 		$this->view->setVar('articleCategories', $articleCategories);
		$this->view->setVar('items', $articleItems);
		$this->view->setVar('pager', $articleQuery->paging->getPagers());
		$this->view->setVar('cid', intval($_GET['cid']));
		$this->view->setVar('articles', $this->getArticleList());
		$this->view->setVar('controller', $this->controller);
	}

	/**
	 * 文章详情
	 */
	public  function  showAction()
	{
		$id = $this->request->get('id','int',0);
		if(empty($id)){
			$this->redirect('');
		}
		
		//新增语言过滤
		$site_id = $this->getCore()->getSiteID();

		//读取文章
		$articleQuery = new SQuery('article as a');
		$articleQuery->join = ' join article_category as c on a.cat_id=c.id ';
		$articleQuery->fields = 'a.*,c.name as cat_name';
		$articleQuery->where = 'a.id='.$id . ' and a.site_id='.$site_id;
		
		$articleItems = $articleQuery->find();


		if(empty($articleItems)){
			$this->redirect('');
		}
		$articleItem = $articleItems[0];


		//读取上一篇
		$articleModel = new SModel('article');
		$preArticleItem = $articleModel->query('cat_id='.$articleItem->cat_id.' and id<'.$id . ' and site_id='.$site_id,'id,title','id','desc',1);
		$preArticleItem = $preArticleItem[0];
		//读取下一篇
		$articleModel = new SModel('article');
		$nextArticleItem = $articleModel->query('cat_id='.$articleItem->cat_id.' and id>'.$id. ' and site_id='.$site_id,'id,title','id','asc',1);
		$nextArticleItem = $nextArticleItem[0];

		//更新次数
		$articleModel->updateBySql('id='.$id, 'clicked = clicked+1');

		$this->view->setVar('next', $nextArticleItem);
		$this->view->setVar('pre', $preArticleItem);
		$this->view->setVar('item', $articleItem);
		$this->view->setVar('seo_title', $articleItem->title);
		$this->view->setVar('seo_keywords', $articleItem->seo_keywords);
		$this->view->setVar('seo_description', $articleItem->seo_description);
		$this->view->setVar('articles', $this->getArticleList());
		$this->view->setVar('controller', $this->controller);

	}

}